﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.WeApp;
using PetaPoco;

namespace BrnMall.Manager.Manager.WeApp
{
    /// <summary>
    /// 小程序模板管理
    /// </summary>
    public static class WeApplicationManager
    {
        #region 第三方平台

        public static ApplicationPlatform GetDefaultApplicationPlatform()
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<ApplicationPlatform>("select * from weapp_platform");
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ticket"></param>
        public static void UpdateComponentVerifyTicket(int id, string ticket)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_platform  set ComponentVerifyTicket=@0,VerifyTicketTime=GETDATE() where id=@1", ticket, id);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="component_access_token"></param>
        /// <param name="component_expires_in"></param>
        public static void UpdateComponentAccessToken(int id, string component_access_token,
            DateTime component_expires_in)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_platform  set component_access_token=@0,component_expires_in=@1 where id=@2", component_access_token,component_expires_in, id);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pre_auth_code"></param>
        /// <param name="pre_expires_in"></param>
        public static void UpdatePreAuthCode(int id, string pre_auth_code,
            DateTime pre_expires_in)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_platform  set pre_auth_code=@0,pre_expires_in=@1 where id=@2", pre_auth_code, pre_expires_in, id);
            }
        }
        #endregion

       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public static void AddApplication(WeApplication app)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(app);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static WeApplication GetWeApplication(int appid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<WeApplication>(appid);
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appid"></param>
        public static void DeleteApplication(int appid)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<WeApplication>(appid);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public static void UpdateApplication(WeApplication app)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(app);
            }
        }
        /// <summary>
        /// 更新模板ID，同时更新对应的小程序模板ID，并且设置小程序为待更新状态
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="templateId"></param>
        public static void UpdateApplicationTemplate(int appid, int templateId)
        {
            using (var db = new QDMallDatabase())
            {
                var sb=new StringBuilder();
                sb.AppendFormat("update weapp_application set TemplateId={0},updatetime=getdate() where appid={1} ",
                    templateId, appid);
                //统一更新待上传的模板实例
                sb.AppendFormat("update weapp_storeapp set TemplateId={0},TemplateUpdateTime=getdate() where appid={1} and devstatus=1 ", templateId, appid);
                //统一更新待已发布的模板实例
                sb.AppendFormat("update weapp_storeapp set TemplateId={0},NeedUpdate=1,TemplateUpdateTime=getdate() where appid={1} and devstatus=5 ", templateId, appid);
                var sql = sb.ToString();
                db.Execute(sql);
            }
        }
        /// <summary>
        /// 获取使用了某个模板的实例
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public static IList<WeApplication> GetWeApplicationsByTemplateId(int templateId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<WeApplication>("select * from weapp_application where TemplateId=@0", templateId);
            }
        }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="pageNumber"></param>
       /// <param name="itemsPerPage"></param>
       /// <returns></returns>
        public static Page<WeApplication> GetApplicatioinPage(int pageNumber, int itemsPerPage = 15)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<WeApplication>(pageNumber, itemsPerPage, "select * from weapp_application");
            }
        }

        public static void UpdateRawPages(int appid, string rawPages)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update weapp_application set RawPages=@0 where appid=@1", rawPages, appid);
            }
        }

    }
}
